<?php
App::uses('AppController', 'Controller');
/**
 * Ramos Controller
 *
 * @property Ramo $Ramo
 */
class RamosController extends AppController {

	
	public $components = array('RequestHandler'); 	//ok
	public $helpers = array('Js');					//ok
	
	function beforeFilter() {
		//$this->loadModel('Estado');			#ok
		//$this->loadModel('Cidade');			#ok
	}
	
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Ramo->recursive = 0;
		$this->set('ramos', $this->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Ramo->exists($id)) {
			throw new NotFoundException(__('Invalid ramo'));
		}
// 		$options = array('conditions' => array('Ramo.' . $this->Ramo->primaryKey => $id));
// 		$this->set('ramos', $this->ramos->find('first', $options));
		
		
		$options = array('conditions' => array('Ramo.' . $this->Ramo->primaryKey => $id));
		
		
		
		
		$ramos = $this->Ramo->find('first', $options);
		$this->set('ramo', $ramos);
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Ramo->create();
			if ($this->Ramo->save($this->request->data)) {
				$this->Session->setFlash(__('The Ramo has been saved.'), 'default', array('class' => 'alert alert-success'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Ramo could not be saved. Please, try again.'), 'default', array('class' => 'alert alert-danger'));
			}
		}

	}
	
	public function listar_cidades_json() {
		$this->layout = false; 	#ok
		if ($this->RequestHandler->isAjax()) {
			$this->set('cidades', $this->Cidade->find('list', array('conditions' =>
					array('Cidade.estado_id' => $this->params['url']['estadoId']),
					'recursive' => -1)
			)); 	#ok
		}
	}#até aqui tudo certo
	
	
/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Ramo->exists($id)) {
			throw new NotFoundException(__('Invalid Ramo'));
			
		}
		
		if ( $this->request->is('put')|| $this->request->is('post')) {
			if ($this->Ramo->save($this->request->data)) {
				$this->Session->setFlash(__('Ramo salvo com sucesso.'), 'default', array('class' => 'alert alert-success'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The Ramo could not be saved. Please, try again.'), 'default', array('class' => 'alert alert-danger'));
			}
		}
		 else {
			$options = array('conditions' => array('Ramo.' . $this->Ramo->primaryKey => $id));
			$this->request->data = $this->Ramo->find('first', $options);
			
		}
	
		
		//$cidades = $this->Ramo->Cidade->find('list');
		//$this->set(compact('cidades'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Ramo->id = $id;
		if (!$this->Ramo->exists()) {
			throw new NotFoundException(__('Invalid Ramo'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Ramo->delete()) {
			$this->Session->setFlash(__('The Ramo has been deleted.'), 'default', array('class' => 'alert alert-success'));
		} else {
			$this->Session->setFlash(__('The Ramo could not be deleted. Please, try again.'), 'default', array('class' => 'alert alert-danger'));
		}
		return $this->redirect(array('action' => 'index'));
	}
}
